package com.saturn.titan.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
	1.   1天前 、1周前、3小时前、12分钟前、2秒前、7 天前 12:01
	2.   2011-08-31
	3.   2011-09-01 06:11
	4.   2011-09-01 15:19:09
	5.   昨天、前天、今天  06:11
	6.   2011年5月1日
	7.   2011年05月01日
	8.   NULL
	9.   2011-9-1
	10.  '0000-00-00 00:00:00'
	11.  刚刚
	12.  很久以前
	13.  MM月DD日 
	14.  09-22 08:49
 */
public class GeneralDateUtil {

	private static Logger logger = LoggerFactory.getLogger(GeneralDateUtil.class);

	/**
	 * 格式化 发表时间
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static Date toDate(String dateStr, Date createTime) {
		if (dateStr == null || "".equals(dateStr)) {
			return null;
		}
		// 	2.   2011-08-31
		//  3.   2011-09-01 06:11
		//  4.   2011-09-01 15:19:09
		//  9.   2011-9-1
		String result = ExtractUtil.getFirstContent(dateStr, "\\d{1,4}-\\d{1,2}-\\d{1,2}\\s*(\\d{1,2}:\\d{1,2}:\\d{1,2}|\\d{1,2}:\\d{1,2})?", 0);
		if (!"".equals(result)) {
			return changeDateForDate(result, createTime);
		}
		//  9.   2011/9/1
		result = ExtractUtil.getFirstContent(dateStr, "\\d{1,4}/\\d{1,2}/\\d{1,2}\\s*(\\d{1,2}:\\d{1,2}:\\d{1,2}|\\d{1,2}:\\d{1,2})?", 0);
		if (!"".equals(result)) {
			return changeEnDateForDate(result, createTime);
		}
		//	6.   2011年5月1日
		//  7.   2011年05月01日
		result = ExtractUtil.getFirstContent(dateStr, "\\d{1,4}年\\d{1,2}月\\d{1,2}日\\s*(\\d{1,2}:\\d{1,2}:\\d{1,2}|\\d{1,2}:\\d{1,2})?", 0);
		if (!"".equals(result)) {
			return changeDateForChDate(result, createTime);
		}
		// 无法解析
		return null;
	}

	private static Date changeEnDateForDate(String date, Date createTime) {
		try {
			date = date.replaceAll("/", "-");
			return formatPostTime(date, createTime);
		} catch (Exception e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	// 1.   1天前 、1周前、3小时前、12分钟前、2秒前
	public static Date changeDateForBefore(String date, Date createTime) {
		try {
			String count = ExtractUtil.getFirstContent(date, "(\\d+)", 1);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			if (date.indexOf("年") != -1) {
				calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - Integer.valueOf(count));
			} else if (date.indexOf("月") != -1) {
				calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - Integer.valueOf(count));
			} else if (date.indexOf("周") != -1) {
				calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - (Integer.valueOf(count) * 7));
			} else if (date.indexOf("天") != -1) {
				calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - Integer.valueOf(count));
			} else if (date.indexOf("小时") != -1) {
				calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) - Integer.valueOf(count));
			} else if (date.indexOf("分钟") != -1) {
				calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - Integer.valueOf(count));
			} else if (date.indexOf("秒") != -1) {
				calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - Integer.valueOf(count));
			}
			return calendar.getTime();
		} catch (Exception e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	//5. 今天、  昨天、前天
	public static Date changeDateForDay(String date, Date createTime) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			if (date.indexOf("昨天") != -1) {
				calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1);
				if (date.length() > 3) {
					String hour = ExtractUtil.getFirstContent(date, "(\\d{1,2}):\\d{1,2}", 1);
					if (!"".equals(hour)) {
						calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
					}
					String mm = ExtractUtil.getFirstContent(date, "\\d{1,2}:(\\d{1,2})", 1);
					if (!"".equals(mm)) {
						calendar.set(Calendar.MINUTE, Integer.valueOf(mm));
					}
				}
			} else if (date.indexOf("前天") != -1) {
				calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 2);
				if (date.length() > 3) {
					String hour = ExtractUtil.getFirstContent(date, "(\\d{1,2}):\\d{1,2}", 1);
					if (!"".equals(hour)) {
						calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
					}
					String mm = ExtractUtil.getFirstContent(date, "\\d{1,2}:(\\d{1,2})", 1);
					if (!"".equals(mm)) {
						calendar.set(Calendar.MINUTE, Integer.valueOf(mm));
					}
				}
			} else if (date.indexOf("今天") != -1) {
				calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
				if (date.length() > 3) {
					String hour = ExtractUtil.getFirstContent(date, "(\\d{1,2}):\\d{1,2}", 1);
					if (!"".equals(hour)) {
						calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
					}
					String mm = ExtractUtil.getFirstContent(date, "\\d{1,2}:(\\d{1,2})", 1);
					if (!"".equals(mm)) {
						calendar.set(Calendar.MINUTE, Integer.valueOf(mm));
					}
				}
			}
			return calendar.getTime();
		} catch (Exception e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	//	2.   2011-08-31
	//  3.   2011-09-01 06:11
	//  4.   2011-09-01 15:19:09
	//  9.   2011-9-1
	public static Date changeDateForDate(String date, Date createTime) {
		try {
			return formatPostTime(date, createTime);
		} catch (Exception e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	//	6.   2011年5月1日
	//  7.   2011年05月01日
	public static Date changeDateForChDate(String date, Date createTime) {
		try {
			date = date.replaceAll("年", "-");
			date = date.replaceAll("月", "-");
			date = date.replaceAll("日", " ");
			return formatPostTime(date, createTime);
		} catch (Exception e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	// 12.  很久以前
	public static Date changeDateForLong(String date, Date createTime) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 3);
			return calendar.getTime();
		} catch (Exception e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	// 13.  MM月DD日 
	public static Date changeDateForChDateOutYear(String date, Date createTime) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(createTime);
			date = calendar.get(Calendar.YEAR) + "-" + date;
			date = date.replaceAll("月", "-");
			date = date.replaceAll("日", " ");
			return formatPostTime(date, createTime);
		} catch (RuntimeException e) {
			logger.warn("发帖时间格式解析异常：" + date, e);
			return null;
		}
	}

	// 14.  09-22 08:49
	public static Date changeDateForDateNotYear(String date, Date createTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(createTime);
		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
		String hour = ExtractUtil.getFirstContent(date, "(\\d{1,2}):\\d{1,2}", 1);
		
		if (!"".equals(hour)) {
			calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(hour));
		}
		
		String mm = ExtractUtil.getFirstContent(date, "\\d{1,2}:(\\d{1,2})", 1);
		if (!"".equals(mm)) {
			calendar.set(Calendar.MINUTE, Integer.valueOf(mm));
		}
		
		String month = ExtractUtil.getFirstContent(date, "(\\d{1,2})-\\d{1,2}", 1);
		if (!"".equals(month)) {
			calendar.set(Calendar.MONTH, Integer.valueOf(month) - 1);
		}
		
		String day = ExtractUtil.getFirstContent(date, "\\d{1,2}-(\\d{1,2})", 1);
		if (!"".equals(day)) {
			calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(day));
		}
		return calendar.getTime();
	}

	/**
	 * 时间格式化
	 * @param dateStr
	 * @return
	 */
	public static Date formatPostTime(String dateStr, Date createTime) {
		dateStr = dateStr.replaceAll("\\s+", " ");
		dateStr = dateStr.replaceAll("　", " ");
		dateStr = dateStr.trim();
		
		try {
			int yearIndex = dateStr.indexOf("-");
			if (yearIndex >= 0) {
				int year = Integer.valueOf(dateStr.substring(0, yearIndex));
				if (yearIndex == 4 && (year < 1970 || year > 2037)) {
					logger.warn("采集的发帖时间年份有误！" + dateStr);
					return null;
				}
				
				if (dateStr.indexOf("-", yearIndex + 1) < 0) {
					Calendar now = Calendar.getInstance();
					now.setTime(createTime);
					dateStr = now.get(Calendar.YEAR) + "-" + dateStr;
				}
				
				if (yearIndex == 2) {
					dateStr = "20" + dateStr;
				}
			}
			
			int index = dateStr.indexOf(":");
			if (dateStr.indexOf(":") < 0) {
				dateStr += " 00:00:00";
			} else if (dateStr.indexOf(":", index + 1) < 0) {
				dateStr += ":00";
			}
			
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = simpleDateFormat.parse(dateStr);
			simpleDateFormat = null;
			if (date1.getTime() > createTime.getTime()) {
				return null;
			}
			return date1;
		} catch (Exception e) {
			logger.warn("发帖时间格式解析失败！" + dateStr);
			return null;
		}
	}

	/**
	 * 修复年份大于当前年份 + 1 导致年份过度增大
	 * @param date
	 * @return
	 */
	public static Date fixPubTime(Date date) {
		if(date == null) return null;
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		Calendar curr = Calendar.getInstance();
		curr.setTime(new Date());
		
		if(calendar.get(Calendar.YEAR) > (curr.get(Calendar.YEAR) + 1)) {
			calendar.set(Calendar.YEAR, curr.get(Calendar.YEAR));
			return calendar.getTime();
		}
		return date;
	}
	
}
